package org.openrbac.rbac.controller.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author bapie
 */
public class BaseController<S extends IService<T>, T> {
    @Autowired
    protected S service;

    @GetMapping("/get")
    public T get(@RequestHeader("id") Long id) {
        return service.getById(id);
    }

    @PostMapping("/plus")
    public T plus(@RequestBody T object) {
        service.save(object);
        return object;
    }

    @PutMapping("/edit")
    public Boolean edit(@RequestBody T object) {
        return service.updateById(object);
    }

    @DeleteMapping("/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return service.removeById(id);
    }

    @GetMapping("/page/{page}/{size}")
    public IPage<T> page(@PathVariable("page") int page, @PathVariable("size") int size) {
        return service.page(new Page<>(page, size));
    }
}
